package com.hnevc.myapplication01;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import com.google.gson.Gson;
import com.hnevc.myapplication01.adapter.WeatherAdapter;
import com.hnevc.myapplication01.bean.WeatherBean;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView wendu;
    private TextView tianqi;
    private TextView fengxiang;
    private TextView fengji;
    private TextView shidu;
    private ListView wt_listview;
    private String url;
    private List<WeatherBean.ResultBean.FutureBean> weatherList = new ArrayList<>();
    private WeatherAdapter weatherAdapter = new WeatherAdapter(weatherList,MainActivity.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();

    }

    private void initView() {
        wendu = (TextView) findViewById(R.id.wendu);
        tianqi = (TextView) findViewById(R.id.tianqi);
        fengxiang = (TextView) findViewById(R.id.fengxiang);
        fengji = (TextView) findViewById(R.id.fengji);
        shidu = (TextView) findViewById(R.id.shidu);
        wt_listview = (ListView) findViewById(R.id.wt_listview);

    }

    private void initData() {
        url = "http://apis.juhe.cn/simpleWeather/query?city=%E9%95%BF%E6%B2%99&key=ca00b82826d5c037e09288133980099e";
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                WeatherBean weatherBean = gson.fromJson(jsonObject.toString(), WeatherBean.class);
                wendu.setText(weatherBean.getResult().getRealtime().getTemperature() + "C`");
                tianqi.setText(weatherBean.getResult().getRealtime().getInfo() + "");
                fengxiang.setText(weatherBean.getResult().getRealtime().getDirect() + "");
                fengji.setText(weatherBean.getResult().getRealtime().getPower() + "");
                shidu.setText(weatherBean.getResult().getRealtime().getHumidity() + "");
                Log.e("TAG", "run: " + weatherBean.getResult().getRealtime().getTemperature());
                for (int i = 0; i < weatherBean.getResult().getFuture().size(); i++) {
                    weatherBean.getResult().getFuture().get(i).setDate(weatherBean.getResult().getFuture().get(i).getDate());
                    weatherBean.getResult().getFuture().get(i).setTemperature(weatherBean.getResult().getFuture().get(i).getTemperature());
                    weatherBean.getResult().getFuture().get(i).setWeather(weatherBean.getResult().getFuture().get(i).getWeather());
                    weatherBean.getResult().getFuture().get(i).setDirect(weatherBean.getResult().getFuture().get(i).getDirect());
                }
                weatherList.addAll(weatherBean.getResult().getFuture());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        weatherAdapter.notifyDataSetChanged();
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
        wt_listview.setAdapter(weatherAdapter);
    }
}